<?php


namespace app\common\model;


use think\Db;
use think\Model;

class ShopModel extends Model
{
    protected $name = 'shop';


    /**
     * 获取店铺列表
     */
    public function getList()
    {
        try {
            $list = $this
                ->field('id,user_id,shopname,mobile,image,address,lat,lng')
                ->order('weigh', 'desc')
                ->select();

            foreach ($list as $row) {
                $row->image = full_url($row->image);
            }

            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $list
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => $e->getLine()
            ];
        }
    }


    /**
     * 商家-自提商品核销
     * @param int $user_id
     * @param string $claim_code 取件码
     */
    public function zitiGoodsVerification($params, $user_id)
    {
        extract($params);
        try {
            $user = model('app\common\model\User')->get($user_id);
            if($user->group_id != 2) {
                throw new \Exception('暂无访问权限',402);
            }
//            $shop = model('app\common\model\ShopModel')->get(['user_id' => $user_id]);
//            if(!$shop) {
//                throw new \Exception('请先绑定门店',49204);
//            }
            $claim_code = strtoupper($claim_code);
            $order = model('app\common\model\order\OrderModel')->get(['claim_code' => $claim_code],['item']);
            if(!$order) {
                throw new \Exception('未找到订单',49201);
            }
            if($order->shop_id != $user_id) {
                throw new \Exception('非本店下单产品',49202);
            }
            if($order->status != 2) {
                throw new \Exception('商品已核销',49203);
            }
            // 订单状态:0=待支付,1=已支付/待发货,2=待收货,3=已收货,4=交易完成,5=已取消
            $order->status = 3;
            $order->claim_shop_id = $user_id;
            $order->received_time = time();
            $order->save();

            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => $order
            ];
        }catch (\Exception $e) {
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => $e->getLine()
            ];
        }
    }


    /**
     * 商家-兑换券核销
     * @param int $user_id
     * @param string $claim_code 取件码
     */
    public function couponExchangeVerification($params, $user_id)
    {
        extract($params);
        Db::startTrans();
        try {
            $user = model('app\common\model\User')->get($user_id);
            if(!in_array($user->group_id,[2,3])) {
                throw new \Exception('暂无访问权限',402);
            }
//            $shop = model('app\common\model\ShopModel')->get(['user_id' => $user_id]);
//            if(!$shop) {
//                throw new \Exception('请先绑定门店',49306);
//            }
            $claim_code = strtoupper($claim_code);
            $user_coupon_exchange = model('app\common\model\UserCouponExchangeModel')->get(['claim_code' => $claim_code]);
            if(!$user_coupon_exchange) {
                throw new \Exception('未找到兑换券',49301);
            }
            if($user_coupon_exchange->shop_id != $user_id) {
                throw new \Exception('非本店下单产品',49202);
            }
            $time = time();
            if($user_coupon_exchange->start_time > $time) {
                throw new \Exception('兑换券未到使用时间',49302);
            }
            if($user_coupon_exchange->end_time < $time) {
                throw new \Exception('兑换券超出使用时间',49303);
            }
            if($user_coupon_exchange->status == 2) {
                throw new \Exception('兑换券已使用',49304);
            }
            if($user_coupon_exchange->status == 3) {
                throw new \Exception('兑换券已过期',49305);
            }
            $user_coupon_exchange->status = 2;
            $user_coupon_exchange->claim_shop_id = $user_id;
//            $user_coupon_exchange->claim_shop_id = $shop->id;
            $user_coupon_exchange->use_time = $time;
            $user_coupon_exchange->save();

            $order = model('app\common\model\order\OrderModel')->get($user_coupon_exchange->order_id);
            // TODO 分销处理
            \app\common\model\UserBrokerageLogModel::checkUserBrokerage('exchange',$order->user_id, $order->id, $user_id, $user_coupon_exchange->id);
//            \app\common\model\UserBrokerageLogModel::checkUserBrokerage('exchange',$order->user_id, $order->id, $shop->id, $user_coupon_exchange->id);

            Db::commit();
            return [
                'code' => 200,
                'msg' => '请求成功',
                'data' => []
            ];
        }catch (\Exception $e) {
            Db::rollback();
            return [
                'code' => $e->getCode(),
                'msg' => $e->getMessage(),
                'data' => $e->getLine()
            ];
        }
    }


}